package com.fitns.user.controller.organ;


import com.fitns.user.conf.Resp;
import com.fitns.user.dto.organ.OrganTree;
import com.fitns.user.dto.organ.OrganVO;
import com.fitns.user.model.organ.Company;
import com.fitns.user.model.organ.Organ;
import com.fitns.user.service.organ.OrganService;
import com.fitns.user.utils.ResultData;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/organ")
@RestController
@Api(description = "组织机构管理")
public class OrganController {

    @Autowired
    private OrganService organService;


    @GetMapping("/searchParentAndChild")
    @ApiOperation("分页查询")
    public Object searchParentAndChild(OrganVO organVO){
        PageInfo<OrganVO> organVOPageInfo= organService.findChildListByParent(organVO);
        organVOPageInfo.setPageSize(organVO.getPageSize());
        return Resp.page(organVOPageInfo);
    }

    @PostMapping
    @ApiOperation("新增")
    public ResultData insert(@RequestBody OrganVO organVO){
        Organ organ=organService.add(organVO);
        return new ResultData(200,"SUCESS",organ,"");

    }

    @PutMapping("/modify")
    @ApiOperation("查询")
    public ResultData modify( OrganVO organVO){
        OrganVO organVO1= organService.modify(organVO);
        return new ResultData(200,"SUCESS",organVO1,"");

    }

    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public ResultData delete(@RequestParam Long id){
        organService.delete(id);
        return new ResultData(200,"SUCESS",null,"");
    }

    @GetMapping("/findCompany")
    @ApiOperation("查找节点下的所有公司，除去节点的直接子节点")
    public ResultData findCompany(OrganVO organVO){
        List<Company> companies= organService.findCompanyByParent(organVO);
        return new ResultData(200,"SUCESS",companies,"");
    }

    @GetMapping("/findOrganTree")
    @ApiOperation("机构树")
    public ResultData findOrganTree(){
        List<OrganTree> organVOS=organService.getOrganTree();
        return new ResultData(200,"SUCESS",organVOS,"");
    }

}
